﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraExport;
using DevExpress.XtraGrid.Export;
using DevExpress.XtraGrid.Views.Grid;
using Wolfut.MirageEE.Domain.CommonUtilities;
using Wolfut.MirageEE.UI.UIBaseComponents;

namespace Wolfut.MirageEE.UI.UICommonUtilities
{
public class GridExportPrintClass
	{
		private GridView m_gridView;
		private MirageEEFormBase m_Form;
		private string exportFileName=string.Empty ;
        DevExpress.XtraPrinting.PrintingSystem ps = new DevExpress.XtraPrinting.PrintingSystem();
		public string ExportFileName
		{
			get
			{
				if(!string.IsNullOrEmpty(exportFileName))
					return this.exportFileName;
				else
					if(m_Form!=null)
				{
                    GenerateExportFileName(m_Form.Text);
				}
				return "export";
			}
			set
			{
				exportFileName=value;
			}
		}
		private DevExpress.XtraGrid.GridControl exportPrintGridControl;
		protected  DevExpress.XtraGrid.GridControl ExportPrintGridControl
		{
			set
			{
				exportPrintGridControl=value;
			}
			get
			{
				return exportPrintGridControl;
			}
		}
		private string title;
		protected string Title
		{
			get
			{
				return this.title;
			}
			set
			{
				title=value;
			}
		}
		private string enCoding="UTF-8";
		protected string EnCoding
		{
			get
			{
				return enCoding;
			}
			set
			{
				enCoding=value;
			}
		}

        public GridExportPrintClass(MirageEEFormBase form)
		{
			m_Form=form;
		}
        public GridExportPrintClass(MirageEEFormBase form, string formBusinessName)
        {
            m_Form = form;
            GenerateExportFileName(formBusinessName);
        }

        public void SetExportFileName(string formBusinessName)
        {
            GenerateExportFileName(formBusinessName);
         }
        private void GenerateExportFileName(string formBusinessName)
         {
             this.ExportFileName = formBusinessName + " " + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year;
         }
		
		public virtual void Print(DevExpress.XtraGrid.GridControl gridControl)
		{
			this.exportPrintGridControl=gridControl;
			DevExpress.XtraPrinting.PrintableComponentLink link=new DevExpress.XtraPrinting.PrintableComponentLink(ps);
			ps.Links.Add(link);
			link.Component=exportPrintGridControl ;	
			link.CreateDocument();
			//ps.PreviewForm.Show();

		}
        public virtual void Export(Constant.ExportPrintOptions options, GridView view)
		{
			string fileName=ExportFileName;
			this.m_gridView =view;
			switch(options)
			{
                case Constant.ExportPrintOptions.EXPORT_TO_HTML:
					fileName = ShowSaveFileDialog("HTML Document", "HTML Documents|*.html" ,fileName);
					break;
                case Constant.ExportPrintOptions.EXPORT_TO_EXCEL:
					fileName = ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls",fileName);
					break;
                case Constant.ExportPrintOptions.EXPORT_TO_TEXT:
					fileName = ShowSaveFileDialog("Text Document", "Text Files|*.txt",fileName);
					break;
				default:
					fileName = ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls",fileName);
					break;
			}
			if(fileName != "") 
			{
				switch(options)
				{
                    case Constant.ExportPrintOptions.EXPORT_TO_HTML:
						ExportTo(new ExportHtmlProvider(fileName));
						break;
                    case Constant.ExportPrintOptions.EXPORT_TO_EXCEL:
						ExportTo(new ExportXlsProvider(fileName));
						break;
                    case Constant.ExportPrintOptions.EXPORT_TO_TEXT:
						ExportTo(new ExportTxtProvider(fileName));
						break;
					default:
						ExportTo(new ExportXlsProvider(fileName));
						break;
				}
				
				OpenFile(fileName);
			}		
		}

		//</sbExportToTXT>

		private void OpenFile(string fileName) 
		{
			if(MessageBox.Show("你想打开文件吗？", "导出...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
			{
				try 
				{
					System.Diagnostics.Process process = new System.Diagnostics.Process();
					process.StartInfo.FileName = fileName;
					process.StartInfo.Verb = "Open";
					process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
					process.Start();
				} 
				catch 
				{
					MessageBox.Show(this.m_Form ,"系统里找不到相应的程序打开导出的数据文件。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 
				}
			}
		}

		//<sbExportToHTML>
		private void ExportTo(IExportProvider provider) 
		{
			Cursor currentCursor = Cursor.Current;
			Cursor.Current = Cursors.WaitCursor;
			BaseExportLink link = m_gridView.CreateExportLink(provider);
			(link as GridViewExportLink).ExpandAll = false;
			StartExport();
			link.Progress += new DevExpress.XtraGrid.Export.ProgressEventHandler(Export_Progress);
			link.ExportTo(true);
			provider.Dispose();
			EndExport();
			link.Progress -= new DevExpress.XtraGrid.Export.ProgressEventHandler(Export_Progress);

			Cursor.Current = currentCursor;
		}
		//</sbExportToHTML>

		private string ShowSaveFileDialog(string title, string filter,string fileName) 
		{
			SaveFileDialog dlg = new SaveFileDialog();
			dlg.Title = "导出到 " + title;
			dlg.FileName = fileName;
			dlg.Filter = filter;
			if(dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
			return "";
		}
		


		ProgressForm_Grid progressForm = null;
		protected  void StartExport() 
	{
		progressForm = new ProgressForm_Grid(m_Form);
		progressForm.Show();
				
	}
		protected  void EndExport() 
	{
		progressForm.Dispose();
		progressForm = null;
	}
		protected  void Export_Progress(object sender, DevExpress.XtraGrid.Export.ProgressEventArgs e) 
	{
		if(e.Phase == DevExpress.XtraGrid.Export.ExportPhase.Link) 
		progressForm.SetProgressValue(e.Position);
	}

}
}
	
